using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using PHSRAG.Insight21.Common.Pages;

namespace PHSRAG.Insight21
{
    /// <summary>
    /// The Error page is invoked anytime an application error occurs. This page is meant to display a user-friendly view of
    /// what blew up.
    /// </summary>
    public partial class Error : BasePage
    {
        #region Instance Variables

        #endregion

        #region Event Handlers

        /// <summary>
        /// Display in the annunciator a user-friendly message why a request has failed.
        /// Failures are categorized broadly into two buckets: HTTP failure, and all others. In the former case, we look for a
        /// specific set of common errors so that they may be mapped to a friendly message. In the worst case, we will show
        /// the actual exception code itself.
        /// To help debug what went wrong, I am also including the stack trace. Once the application moves to production, we could
        /// take that part out.
        /// </summary>
        /// <param name="sender">Event source</param>
        /// <param name="e">Argument associated with the event</param>
        protected void OnPageLoad(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            Exception exception = Server.GetLastError();
            if (exception is HttpException)
            {
                int code = ((HttpException) exception).GetHttpCode();
                switch (code)
                {
                    case 403:
                        sb.AppendFormat("{0}You are not allowed to view that page", sb.Length > 0 ? "<br>" : "");
                        break;
                    case 404:
                        sb.AppendFormat("{0}The page you have requested can't be found", sb.Length > 0 ? "<br>" : "");
                        break;
                    case 408:
                        sb.AppendFormat("{0}The request has timed out", sb.Length > 0 ? "<br>" : "");
                        break;
                    case 500:
                        sb.AppendFormat("{0}The server can't fulfill your request", sb.Length > 0 ? "<br>" : "");
                        break;
                    case 503:
                        sb.AppendFormat("{0}The server is experiencing a heavy load", sb.Length > 0 ? "<br>" : "");
                        break;
                    default:
                        sb.AppendFormat("{0}The server has experienced an error {1}", sb.Length > 0 ? "<br>" : "", code);
                        break;
                }
            }
            else
            {
                sb.Append(exception.ToString());
            }

            if (Convert.ToBoolean(Application[Global.DisplayStackTraceKey]))
            {
                sb.AppendFormat("<br><br>Stack Trace:<br>{0}", Regex.Replace(exception.StackTrace, @"\r\n", "<br>"));
            }

            annunciator.Title = "The following error(s) occurred while service the current request:";
            annunciator.Message = sb.ToString();

            Server.ClearError();
        }

        #endregion

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.OnPageLoad);
        }

        #endregion
    }
}